package com.my.home.modules.controller;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.my.home.common.Base.BaseController;
import com.my.home.common.Base.RespJson;
import com.my.home.common.shiro.ShiroUser;
import com.my.home.modules.entity.User;
import com.my.home.modules.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Al Sah-him on 2018/5/25.
 */
@Controller
@RequestMapping(value = "/user")
public class UserContoller extends BaseController {

    private final UserService userService;

    @Autowired
    public UserContoller(UserService userService) {
        this.userService = userService;
    }

    /**
     * 首页
     *
     * @param
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "")
    public String index(Model model) {
        model.addAttribute("userList", userService.selectList(null));
        Subject subject = SecurityUtils.getSubject();
        ShiroUser user = (ShiroUser) subject.getPrincipal();
//        System.out.println(SecurityUtils.getSubject().isPermitted("admin"));
        System.out.println(user.toString());
//        if (SecurityUtils.getSubject().isAuthenticated() == false) {
//            return "redirect:/login";
//        }
        return FRONT_PATH + "/index";
    }

    /**
     * 新增页面
     *
     * @param modelAndView
     * @param id
     * @return
     */
    @RequestMapping(value = "/preSave")
    public String preSave(ModelAndView modelAndView, @RequestParam(value = "id", required = false) Long id) {
        if (id != null) {
            modelAndView.addObject("user", userService.getById(id.toString()));
        }
        return FRONT_PATH + "/save";
    }

    /**
     * 保存
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String, Object> save(User user) {
        if (user.getId() != null) {
            userService.updateById(user);
        } else {
            userService.insert(user);
        }
        return RespJson.success();
    }


    /**
     * 测试
     *
     * @return
     */
    @RequestMapping(value = "/test")
    @ResponseBody
    public Map<String,Object> test() {
        try {
            FastJsonJsonView view = new FastJsonJsonView();
            Map<String, Object> map = new HashMap<>();
            map.put("code", "1111");
            map.put("msg", "呃呃呃");
            throw new Exception();
        } catch (Exception e) {
            throw new RuntimeException("出错啦");
        }
    }
}
